﻿//-----------------------------------------------------------------------------------------------------------
// Wbfs Engine (WBFSe) Project by Omega Frost 
// http://wbfse.codeplex.com/
//
// WBFSe is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using WBFSe.Attributes;
using WBFSe.Builders;
using WBFSe.FormatApi;

namespace WBFSe.IO.Files
{
    /// <summary>
    /// A temporary creation-only wii game file
    /// </summary>
    class WbfsTempFile : IWbfsFileInfo
    {
        // Variables

        private IWbfsFileInfo source;
        private IWbfsFormat format;
        private WbfsCreator creator;
        private IDiscExtendedInfo info;

        // Properties

        /// <summary>
        /// Indicates that the file
        /// is being created by engine
        /// and may be deleted on failure
        /// </summary>
        public bool Temporary
        {
            get { return true; }
        }

        /// <summary>
        /// Number of open streams
        /// </summary>
        public int ActiveStreams
        {
            get { return 0; }
        }

        /// <summary>
        /// Name of the game
        /// </summary>
        public string Name
        {
            get { return this.source.Name; }
        }

        /// <summary>
        /// Code of the game
        /// </summary>
        public string Code
        {
            get { return this.source.Code; }
        }

        /// <summary>
        /// Region of the game
        /// </summary>
        public Region Region
        {
            get { return this.source.Region; }
        }

        /// <summary>
        /// Type of the game
        /// </summary>
        public GameType Type
        {
            get { return GameType.Wii; }
        }

        /// <summary>
        /// Physical location of the container
        /// </summary>
        public String Path
        {
            get { return this.creator.Path; }
        }

        /// <summary>
        /// First container filename
        /// </summary>
        public String Filename
        {
            get { return this.creator.Filenames[0]; }
        }

        /// <summary>
        /// Container filenames
        /// </summary>
        public IList<string> Filenames
        {
            get { return this.creator.Filenames; }
        }

        /// <summary>
        /// Physical size of the container
        /// </summary>
        public long Size
        {
            get { return this.creator.EstimatedSize; }
        }

        /// <summary>
        /// Sector size used by the enclosing type
        /// </summary>
        public int SectorSize
        {
            get { return this.creator.SectorSize; }
        }

        /// <summary>
        /// Usage table of the enclosing type
        /// </summary>
        public UsageTable Usage
        {
            get { return this.creator.SectorUsage; }
        }

        /// <summary>
        /// Format of the container
        /// </summary>
        public IWbfsFormat Format
        {
            get { return this.format; }
        }

        // Methods

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="source">The source game</param>
        /// <param name="format">The container format</param>
        /// <param name="creator">The container creator</param>
        /// <param name="extendedInfo">The source info</param>
        public WbfsTempFile(IWbfsFileInfo source, IWbfsFormat format, 
            WbfsCreator creator, IDiscExtendedInfo extendedInfo)
        {
            this.source = source;
            this.format = format;
            this.creator = creator;
            this.info = extendedInfo;
        }

        /// <summary>
        /// Finishes the format creator
        /// </summary>
        /// <param name="successful">The operation was successful</param>
        public void Consolidate(bool successful)
        {
            this.creator.End(successful);
        }

        /// <summary>
        /// Generates extended information for the game
        /// </summary>
        /// <param name="flags">Build flags</param>
        /// <param name="info">Out info</param>
        /// <returns>Disc's extended information</returns>
        /// <remarks>For temporary files, it returns the extended info of the source file</remarks>
        public IDiscExtendedInfo BuildExtendedInfo(BuildFlags flags)
        {
            return this.info;
        }

        /// <summary>
        /// Creates a stream with desired access and share
        /// </summary>
        /// <param name="access">Access for all files of the container</param>
        /// <param name="share">Share for all files of the container</param>
        /// <returns>Stream on success</returns>
        /// <exception cref="System.Exception">On operation failure</exception>
        public IWbfsStream Open(FileAccess access, FileShare share)
        {
            return this.creator.CreateStream();
        }

        /// <summary>
        /// Changes the game data
        /// </summary>
        /// <param name="name">New name</param>
        /// <param name="code">New code</param>
        /// <exception cref="System.NotSupportedException">The file is temporary</exception>
        public void ChangeDiscHeader(string name, string code)
        {
            throw new NotSupportedException("Cannot generate extended information for temporary files");
        }

        /// <summary>
        /// Extracts files from the disc
        /// </summary>
        /// <param name="partition">Partition index</param>
        /// <param name="path">Full path of the file</param>
        /// <param name="output">Stream for data output</param>
        /// <exception cref="System.NotSupportedException">The file is temporary</exception>
        public void ExtractFile(int partition, string path, Stream output)
        {
            throw new NotSupportedException("Cannot generate extended information for temporary files");
        }
    }
}
